package com.crm.system.service.impl;

import com.crm.dto.DepartmentDTO;
import com.crm.dto.UserDTO;
import com.crm.entity.SysUser;
import com.crm.system.mapper.SysUserMapper;
import com.crm.model.UserVO;
import com.crm.system.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper mapper;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;



    @Override
    public SysUser getByLoginName(String loginName) {
        return mapper.getByLoginname(loginName);
    }

    @Override
    public SysUser getByUserId(String userId) {
        return mapper.getByUserId(userId);
    }

    @Override
    public int add(SysUser user) {
        user.setLoginPass(passwordEncoder.encode(user.getLoginPass()));
        return mapper.insert(user);
    }

    @Override
    public int password(String loginName, String loginPass) {
        String pass = passwordEncoder.encode(loginPass);
        return mapper.password(loginName, pass);
    }


    @Override
    public PageInfo<SysUser> list(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<SysUser> sysUsers = mapper.list();
        return new PageInfo<>(sysUsers);
    }

    @Override
    public PageInfo<SysUser> listDep(int pageIndex, int pageSize, String depId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<SysUser> sysUsers = mapper.listDep(depId);
        return new PageInfo<>(sysUsers);

    }

    @Override
    public List<SysUser> listDepUser(String depId) {
        List<SysUser> sysUsers = mapper.listDep(depId);
        return sysUsers;

    }


    @Override
    public int update(SysUser user) {
        return mapper.update(user);
    }

    @Override
    public int remove(String loginName) {
        return mapper.remove(loginName);
    }

    @Override
    public int confirmLoginName(String loginName, String telephone, String dingDing) {
        return mapper.confirmLoginName(loginName, telephone, dingDing);
    }

    @Override
    public SysUser findFromUserID(String userId) {
        return mapper.findFromUserID(userId);
    }

    @Override
    public Collection<UserVO> findDepartmentUser() {
        List<UserDTO> userList = mapper.findDepartmentUser();
        Map<String, UserVO> map = new HashMap<>();
        for (UserDTO user : userList) {
            UserVO model = null;
            if (map.containsKey(user.getDepId())) {
                model = map.get(user.getDepId());

            } else {
                model = new UserVO(user.getDepId(), user.getDepName());
                map.put(model.getDepId(), model);
            }
            List<UserVO.SimpleUser> users = model.getUserList();
            users.add(new UserVO.SimpleUser(user.getUserId(), user.getTrueName()));
        }
        return map.values();

    }

    @Override
    public List<DepartmentDTO> findDepartmentUserList() {
        return mapper.findDepartmentUserList();
    }

    @Override
    public int backUser(String userId) {
        return mapper.backUser(userId);
    }

    @Override
    public String pass(String loginName) {
        return mapper.pass(loginName);
    }

    @Override
    public int roleId(String userId, String roleId) {
        return mapper.roleId(userId, roleId);
    }

    @Override
    public List<SysUser> findAllSystemUser() {
        return mapper.findAllSystemUser();
    }

    @Override
    public SysUser findUserById(String userId) {
        return mapper.findUserById(userId);
    }

    @Override
    public List<SysUser> findAllLeader() {
        return mapper.findAllLeader();
    }
}